<?php

/**
 * Description of Menu
 *
 * @author cybill
 */
class Admin_Model_DbTable_Menu extends Zend_Db_Table_Abstract {
    protected $_name = 'menu';
    protected $_id = 'menuID';
    
    public function findRow($where) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name);
        
        if (is_array($where)) {
            foreach ($where as $key => $value) {
                $sql->where($key, $value);
            }
        }
        
        return $this->_db->fetchRow($sql);
    }
    
    public function findAll($where, $options) {
        $sql = $this->getAdapter()->select()
                ->from(array('a'=>'acl'))
                ->joinLeft(array('b'=>$this->_name), 'b.menuID = a.menuID');
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
        
        if (is_array($options)) {
            if (isset($options['limit']))
                foreach ($options['limit'] as $key => $value) {
                    $sql->limit($key, $value);
                }
        }
        
        return $this->_db->fetchAll($sql);
    }
    
    public function countRows($where) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, 'COUNT(menuID)');                
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
            
        return $this->getAdapter()->fetchOne($sql);
    }
    
    public function doSave($data = array()) {                
        $saveMenu = $this->getAdapter()->insert($this->_name, $data);
        
        $group = $this->_db->fetchAll($this->_db->select()
                    ->from('group', array('groupID')));
        
        $menu = $this->_db->fetchRow($this->_db->select()
                    ->from('menu', array('menuID', 'create_by', 'create_on'))
                    ->where('name = ?', $data['name'])
                    ->where('link = ?', $data['link']));
        
            foreach($group as $item) {
                $data2[] = array(
                    'groupID' => $item['groupID'],
                    'menuID' => $menu['menuID'],
                    'create_by' => $menu['create_by'],
                    'create_on' => $menu['create_on'],
                    'status' => false
                );
            }
            
            foreach($data2 as $item)
                $saveAcl = $this->getAdapter()->insert('acl', $item);
        
        return ($saveMenu && $saveAcl);
        
    }
    
    public function doUpdate($data = array(), $where = '') {        
        return $this->getAdapter()->update($this->_name, $data, array("$this->_id = ?" => $where));
    }
    
    public function doDelete($where) {
        return $this->getAdapter()->delete($this->_name, array("$this->_id = ?" => $where));
    }        
    
    public function findGroupMenu($groupMenu) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, array('menuID', 'name'))                
                ->where('gmenuID = ?', $groupMenu);
        
        return $this->getAdapter()->fetchAll($sql);
    }
    
    public function lookupParentMenu($groupMenu) {
        $parent = array();
        $sql = $this->getAdapter()->select()
                ->from($this->_name, array('menuID', 'name'))
                ->where('gmenuID = ?', $groupMenu)
                ->where('parentID = ?', '0');
        
        $data = $this->getAdapter()->fetchAll($sql);
                
//        foreach($data as $key => $value) {
//            $tdata = array();            
//            $tdata['child'] = array('menuID'=>$value['menuID'], 'name'=>$value['name']);
//            
//            $tdata['parent'] = (!empty($value['parentID']) ? $this->getAdapter()->fetchRow($this->getAdapter()->select()
//                ->from($this->_name, array('menuID', 'name'))) : array());                                      
//            
//            $parent[] = $tdata;
//        }
        
        return $data;
    }    
    
    public function getChildMenu($groupID, $menuID) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name)                
                ->where('parentID = ?', $menuID);
        
        $child = $this->getAdapter()->fetchAll($sql);
        
        foreach ($child as $key=>$value) {
            $sql2 = $this->getAdapter()->select()
                ->from('acl', array('groupID', 'status'))
                ->where('groupID = ?', $groupID)
                ->where('menuID = ?', $value['menuID']);
            
            $status = $this->getAdapter()->fetchRow($sql2);
            
            $child[ $key ]['groupID'] = $status['groupID'];
            $child[ $key ]['status'] = $status['status'];
        }
        
        return $child;
    }
    
    public function getMenuForAcl() {
        $sql = $this->getAdapter()->select()
                ->from($this->_name, array('menuID'));
        
        return $this->_db->fetchAll($sql);
    }
    
    public function findAllMenu($where, $options) {
        $sql = $this->getAdapter()->select()
                ->from($this->_name);
        
        if (is_array($where)) {
            foreach($where as $key=>$value) {
                $sql->where($key, $value);
            }
        }
        
        if (is_array($options)) {
            if (isset($options['limit']))
                foreach ($options['limit'] as $key => $value) {
                    $sql->limit($key, $value);
                }
        }
        
        return $this->_db->fetchAll($sql);
    }
}

?>
